<?php
/**
 * Customer Cutover Interface
 */

// Load Custover Information
use Fox\SalesDistribution\Sales\Partner\Role;
use Fox\Common\Customer\Partner;
use Fox\Html\TextArea;
use Fox\Html\Input;
use Fox\Html\Screen;
use Fox\Common\Customer;
use Fox\Html\CheckBox;
set_time_limit(9500);

$screen = new Screen("cutover","execute");
$block = new Fox\Html\Block("bCutover","Selection Criteria");
$block->add(new TextArea(array("comments","customername")));
$block->add(new CheckBox("commit"));
$block->add(new Input("dbcnt",true,100));
$screen->add($block);
if($screen->submitted())
{
	$inputs = $screen->get();

	if(!$screen->errors())
	{
		$query = \Fox\Dao::infos(Customer::className())->handle()->createQueryBuilder();
		$query->select('*')->from('cutover_customers','t')->setMaxResults($inputs['dbcnt']);
		$fragments[] = $block->item('customername')->fragments();
		$fragment = Fox\Dao\SqlFragment::combine($fragments);
		$result = $fragment->pushIn($query)->execute()->fetchAll(PDO::FETCH_ASSOC);
		$commit = $inputs['commit'];

		foreach($result as $data)
		{
			$keyname = Fox\Utilities::cleanup($data['customername']);
			// Check if the customer already exist
			if(!isset($customers[$keyname]))
			{
				$customer = Customer::findByNameAndType($keyname,Customer::TYPE_BASIC);
				$customers[$keyname] = $customer;
			}
			if(!$customers[$keyname]->exists())
			{
				$customerData = $data;
				if(empty($data['contactname']))
				{
					// There is no contact it could be an adress
					if(!empty($data['function']))
					{
						$customerData['street'] = $data['function'];
						unset($customerData['function']);
					}

				}
				else
				{
					$customerData['function'] = null;
				}
				$customerData['name1'] = $data['customername'];
				$customers[$keyname]->setFields($customerData);
				try {
					Fox\Message::Information("Customer {$data['customername']} has been created");
					if($commit)
					{
						$customers[$keyname]->create();
					}
				}
				catch (Fox\Object\Exception\Save $e)
				{
					var_dump($e);
					Fox\Message::Error($e->getMessage());
				}
			}


			if(!empty($data['contactname']))
			{
				// Need to check if the contact as already been created
				$contact = new Customer();
				$contact->setFields(array("name1" => $data['contactname']));
				$partner = $customers[$keyname]->isPartner($contact);
				if($partner->exists())
				{
					// We need to check if something has been added
					if(!empty($data['phone1']))
						$partner->phone1 = $data['phone1'];
					if(!empty($data['phone2']))
						$partner->phone2 = $data['phone2'];
					if(!empty($data['faxnum']))
						$partner->faxnum = $data['faxnum'];
					if(!empty($data['email1']))
						$partner->email1 = $data['email1'];
					if($commit)
					{
						try {
							$partner->prepareUpdate(Customer::CHANGE_DEFAULT);
							if($partner->update())
							{
								Fox\Message::Information("Partner $partner->name1 data has been updated");
							}
						}
						catch (Fox\Object\Exception\Save $e)
						{
							var_dump($e);
							Fox\Message::Error($e->getMessage());
						}
					}
					elseif($partner->changed())
					{
						Fox\Message::Information("Partner $partner->name1 data has been updated");
					}
				}
				else
				{
					Fox\Message::Information("Partner $partner->name1 has been created and added to $keyname ");
					// It's a new contact it will be added to the customer
					$data['name1'] = $data['contactname'];
					$partner->setFields($data);
					if($commit)
					{
						try {
							$partner->type = Customer::TYPE_CONTACT;
							$partner->create();
							$partnerid = $partner->getId();
							$partnerLink = new Partner();
							$partnerLink->setFields(array(	"partnerid" => $partnerid,
															"role" => Role::CONTACT_PERSON,
															"defaultflag" => 0));

							$customers[$keyname]->partner($partnerLink);
							$customers[$keyname]->prepareUpdate(Customer::CHANGE_DEFAULT);
							$customers[$keyname]->update();
						}
						catch (Fox\Object\Exception\Save $e)
						{
							var_dump($e);
							Fox\Message::Error($e->getMessage());
						}
					}
				}
			}
			else
			{
				// Customer Already Exist and there is no contact information
				// we will check if there are additional information
				if(!empty($data['phone1']))
					$customers[$keyname]->phone1 = $data['phone1'];
				if(!empty($data['phone2']))
					$customers[$keyname]->phone2 = $data['phone2'];
				if(!empty($data['faxnum']))
					$customers[$keyname]->faxnum = $data['faxnum'];
				if(!empty($data['email1']))
					$customers[$keyname]->email1 = $data['email1'];

				if($commit)
				{
					try {
						$customers[$keyname]->prepareUpdate(Customer::CHANGE_DEFAULT);
						if($customers[$keyname]->update())
						{
							Fox\Message::Information("Customer $keyname data has been updated");
						}
					}
					catch (Fox\Object\Exception\Save $e)
					{
						var_dump($e);
						Fox\Message::Error($e->getMessage());
					}
				}
				else
				{
					if($customer->changed())
					{
						Fox\Message::Information("Customer $keyname data has been updated");
					}
				}
			}
		}
	}
}
echo $screen->Display();